﻿using IMModel.Domain.Common;
using System.Linq;
using System.Diagnostics.Contracts;
using System.Collections.Generic;
namespace IMModel.Domain
{
    /// <summary>
    /// Получает объёмы градаций для показателя 1.1.1
    /// </summary>
    class GradationVolumeFunctionOrder111 : DefaultGradationVolumeFunction
    {
        new public static readonly GradationVolumeFunctionOrder111 Instance = 
            new GradationVolumeFunctionOrder111();

        /// <summary>
        /// Получает объёмы по всем градациям показателя
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sender"></param>
        /// <returns></returns>
        public override RespondentIndexCollection GetVolumes(IndexContext сontext,
            CharacteristicTerm contextTerm, Term sender)
        {
            Contract.Assert(сontext != null);
            Contract.Assert(contextTerm != null);
            Contract.Assert(sender != null);

            RespondentIndexCollection result = new RespondentIndexCollection();
            var a = GetFiltredAnswers(сontext, contextTerm, sender);
            var coll = a.GroupBy(x => x.Respondent.GetMeInOtherTerm(sender));

            foreach (var v in coll)
                result.Add(v.Key, v.Sum(x=>x.Volume * x.CalcGradationWeight())
                    / v.Sum(x=>x.Volume));

            return result;
        }
    }
}